package com.lysj.admin.utils.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomUtil {

	private static DateFormat format = new SimpleDateFormat("yyMMdd");
	private static DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
	private static Random random = new Random();

	/**
	 * 生成订单号
	 * 
	 * @return
	 */
	public static String createOrderCode(int length) {
		StringBuilder sb = new StringBuilder(format1.format(new Date()));
		sb.append(getRandomNum(length));
		return sb.toString();
	}

	/**
	 * 生成特定位数的随机数字
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNum(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			val += String.valueOf(random.nextInt(10));
		}
		return val;
	}

	/**
	 * 生成一个范围内的数字
	 */
	public static int getRandomNum(int begin, int end) {
		return (int) ((end - begin) * 1.0 * Math.random()) + begin;
	}

	/**
	 * 生成一个范围内的字符串
	 */
	public static String getRandomString(int length) {
		String str = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(36);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * @author zk
	 * @date 2018-05-30 12:01
	 * @Description 生成0-max的随机数
	 */
	public static int randomNum(int max){
		return random.nextInt(max);
	}
}
